import { mount } from '@vue/test-utils'
import Login from '@/views/login/index.vue'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import router from '@/router'

jest.mock('@/stores/user', () => ({
  useUserStore: jest.fn(() => ({
    login: jest.fn(),
    library_users: { userId: 1 }
  }))
}))

describe('登录功能测试套件', () => {
  let wrapper
  let userStore

  beforeEach(() => {
    userStore = useUserStore()
    wrapper = mount(Login)
    router.push = jest.fn()
  })

  test('管理员登录应跳转至后台', async () => {
    // 模拟管理员角色
    userStore.login.mockResolvedValue({ 
      data: { roles: ['admin'], id: 1 }
    })
    
    await wrapper.find('input[type="text"]').setValue('admin')
    await wrapper.find('input[type="password"]').setValue('admin123')
    await wrapper.find('button').trigger('click')

    expect(router.push).toHaveBeenCalledWith('/dashboard')
  })

  test('普通用户登录应跳转至前台', async () => {
    userStore.login.mockResolvedValue({
      data: { roles: ['user'], id: 2 }
    })
    
    await wrapper.find('input[type="text"]').setValue('user')
    await wrapper.find('input[type="password"]').setValue('user123')
    await wrapper.find('button').trigger('click')

    expect(router.push).toHaveBeenCalledWith('/front')
  })
})
